package com.doule.farmerassistant.plan;

import com.doule.farmerassistant.R;

import android.app.Dialog;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TimePicker;

/**
 * Set Date and time dialog.
 * 
 * @author riguang
 * 
 */
public class SetDateDialog implements OnClickListener {

    /**
     * CreatePlanActivity.
     */
    private CreatePlanActivity mContext;

    /**
     * Set date dialog.
     */
    private Dialog mSetDateDialog;

    /**
     * Show other time view.
     */
    private LinearLayout showOtherTime;

    /**
     * Time type radio button..
     */
    private RadioButton everydayRB, onetimeRB, otherRB;
    // private RadioButton mondayCB, thesdayCB, wednesdayCB, thursdayCB,
    // fridayCB,
    // saturdayCB, sundayCB;
    /**
     * TimePicker.
     */
    private TimePicker mTimePicker;

    /**
     * Save day.
     */
    private String day;

    /**
     * Time type.
     */
    private static final int EVERY_DAY = 1, ONE_TIME = 2, OTHER = 3;

    /**
     * Constructor.
     * 
     * @param context
     *            CreatePlanActivity
     */
    public SetDateDialog(CreatePlanActivity context) {
        this.mContext = context;
        showDialog();
    }

    /**
     * Show dialog.
     */
    private void showDialog() {
        mSetDateDialog = new Dialog(mContext, R.style.Theme_dialog3);
        mSetDateDialog.setContentView(R.layout.dialog_set_plan_time);
        mSetDateDialog.getWindow().setLayout(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT);
        mSetDateDialog.findViewById(R.id.btn_ok).setOnClickListener(this);
        mSetDateDialog.findViewById(R.id.btn_back).setOnClickListener(this);
        mSetDateDialog.findViewById(R.id.btn_gone_ok).setOnClickListener(this);
        mSetDateDialog.findViewById(R.id.btn_gone_back)
                .setOnClickListener(this);
        showOtherTime = (LinearLayout) mSetDateDialog
                .findViewById(R.id.ll_other_time);
        // mondayCB = (RadioButton) mSetDateDialog.findViewById(R.id.rb_monday);
        // thesdayCB = (RadioButton)
        // mSetDateDialog.findViewById(R.id.rb_thesday);
        // wednesdayCB = (RadioButton) mSetDateDialog
        // .findViewById(R.id.rb_wednesday);
        // thursdayCB = (RadioButton) mSetDateDialog
        // .findViewById(R.id.rb_thursday);
        // fridayCB = (RadioButton) mSetDateDialog.findViewById(R.id.rb_friday);
        // saturdayCB = (RadioButton) mSetDateDialog
        // .findViewById(R.id.rb_saturday);
        // sundayCB = (RadioButton) mSetDateDialog.findViewById(R.id.rb_sunday);
        everydayRB = (RadioButton) mSetDateDialog
                .findViewById(R.id.rb_everyday);
        onetimeRB = (RadioButton) mSetDateDialog.findViewById(R.id.rb_onetime);
        otherRB = (RadioButton) mSetDateDialog.findViewById(R.id.rb_other);
        otherRB.setOnClickListener(this);
        everydayRB.setOnClickListener(this);
        onetimeRB.setOnClickListener(this);
        mTimePicker = (TimePicker) mSetDateDialog
                .findViewById(R.id.timePicker1);
        mTimePicker.setIs24HourView(true);
        RadioGroup mRadioGroup = (RadioGroup) mSetDateDialog
                .findViewById(R.id.rg_other);
        mRadioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup arg0, int checkedId) {
                // TODO Auto-generated method stub
                RadioButton nRadioButton = (RadioButton) mSetDateDialog
                        .findViewById(checkedId);
                day = nRadioButton.getText().toString();
            }

        });
        mSetDateDialog.show();
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
        case R.id.rb_onetime:
            showOtherTime.setVisibility(View.GONE);
        case R.id.rb_everyday:
            showOtherTime.setVisibility(View.GONE);
            break;
        case R.id.rb_other:
            showOtherTime.setVisibility(View.VISIBLE);
            break;
        case R.id.btn_gone_ok:
        case R.id.btn_gone_back:
            showOtherTime.setVisibility(View.GONE);
            break;
        case R.id.btn_ok:
            final int minPerHour = 60;
            int hour = mTimePicker.getCurrentHour();
            int min = mTimePicker.getCurrentMinute();
            int timeInt = hour * minPerHour + min;
            int type = 0,
            date = 0;
            String timeString = hour + ":" + min;
            if (everydayRB.isChecked()) {
                type = EVERY_DAY;
            } else if (onetimeRB.isChecked()) {
                type = ONE_TIME;
            } else if (otherRB.isChecked()) {
                type = OTHER;
                if (day == null) {
                    Toast.makeText(mContext,
                            mContext.getString(R.string.please_select_date),
                            Toast.LENGTH_LONG).show();
                } else if (day.equals(mContext.getString(R.string.monday))) {
                    date = 1;
                } else if (day.equals(mContext.getString(R.string.thesday))) {
                    date = 2;
                } else if (day.equals(mContext.getString(R.string.wednesday))) {
                    date = 3;
                } else if (day.equals(mContext.getString(R.string.thursday))) {
                    date = 4;
                } else if (day.equals(mContext.getString(R.string.friday))) {
                    date = 5;
                } else if (day.equals(mContext.getString(R.string.saturday))) {
                    date = 6;
                } else if (day.equals(mContext.getString(R.string.sunday))) {
                    date = 7;
                }
            }
            mContext.setDateInformation(type, date, timeInt, day, timeString);
        case R.id.btn_back:
            if (mSetDateDialog != null) {
                mSetDateDialog.dismiss();
                mSetDateDialog = null;
            }
        default:
            break;
        }
    }
}
